package com.example.rabbitmq7.config;

import com.example.rabbitmq7.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TransConfig {
    @Bean("transExchange")
    public Exchange transExchange() {
        return ExchangeBuilder.directExchange(Constants.TRANS_EXCHANGE).build();
    }

    @Bean("transQueue")
    public Queue transQueue() {
        return QueueBuilder.durable(Constants.TRANS_QUEUE).build();
    }

    @Bean("transBinding")
    public Binding transBinding(@Qualifier("transExchange") Exchange exchange, @Qualifier("transQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("trans").noargs();
    }

    @Bean("transactionManager")
    public RabbitTransactionManager transactionManager(ConnectionFactory factory) {
        return new RabbitTransactionManager(factory);
    }

    @Bean("transactionRabbitTemplate")
    public RabbitTemplate transactionRabbitTemplate(ConnectionFactory factory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(factory);
        rabbitTemplate.setChannelTransacted(true);
        return rabbitTemplate;
    }
}
